import { PerformanceObserver, performance } from "node:perf_hooks";

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration);
});

obs.observe({ type: "measure" });
performance.measure("Start to Now");

performance.mark("A");
(async function doSomeLongRunningProcess() {
  await defer(1000);
  performance.measure("A to Now", "A");

  performance.mark("B");
  performance.measure("A to B", "A", "B");
})();

/**
 * @param {number} delay
 * @returns {Promise<void>}
 */
async function defer(delay) {
  return new Promise((r) => {
    setTimeout(r, delay);
  });
}

/**
 * @param {number} delay
 * @returns {Promise<void>}
 */
async function defer2(delay) {
  return void Atomics.wait(new Int32Array(0), 0, 0, delay);
}
